package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.dto.TeacherListDTO;
import com.demo.pojo.entity.Teacher;
import com.demo.service.TeacherService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Author：潘建冰
 * Package：com.demo.controller
 * Project：demo
 * name：TeacherController
 * Date：2025/9/11  17:20
 */

@RestController
@RequestMapping("/teacher")
public class TeacherController {


    private final TeacherService teacherService;

    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }


    /**
     * 根据id查询教师数据
     * @param id 教师id
     * @return Result
     */
    @GetMapping("/select")
    public Result<Teacher> select(@RequestParam Integer id){
        return Result.success(teacherService.selectById(id));
    }


    /**
     * 保存教师数据
     * @param teacher 教师数据
     * @return  Result
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody Teacher teacher){
        teacherService.saveTeacher(teacher);
        return Result.success();
    }


    /**
     * 修改教师数据
     * @param teacher 教师数据
     * @return  Result
     */
    @PutMapping("/update")
    public Result<Void> update(@RequestBody Teacher teacher){
        teacherService.updateTeacher(teacher);
        return Result.success();
    }

    /**
     * 删除教师数据
     * @param id 教师id
     * @return  Result
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam Integer id){
        teacherService.removeById(id);
        return Result.success();
    }

    /**
     * 分页查询教师数据
     * @param pageNum 页码
     * @param pageSize 每页数据量
     * @param teNo 教师工号
     * @param teName 教师姓名
     * @param tePhone 教师手机号
     * @return  Result
     */
    @GetMapping("/listPage")
    public Result<Map<String, Object>> listPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam int teNo,
                                @RequestParam String teName,
                                @RequestParam Integer teSex,
                                @RequestParam String tePhone){
        //封装DTO对象进行传递
        //TODO 用建造者模式更美观，这里方便阅读
        TeacherListDTO teacherListDTO = new TeacherListDTO();
        teacherListDTO.setTeNo((long) teNo);
        teacherListDTO.setTeName(teName);
        teacherListDTO.setTeSex(teSex);
        teacherListDTO.setTePhone(tePhone);
        teacherListDTO.setPageNum(pageNum);
        teacherListDTO.setPageSize(pageSize);
        return Result.success(teacherService.listPage(teacherListDTO));
    }

}
